// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_UI_UTIL_CORE_TEXT_UTIL_H_
#define IOS_CHROME_BROWSER_UI_UTIL_CORE_TEXT_UTIL_H_

#import <CoreText/CoreText.h>
#import <UIKit/UIKit.h>

#import "base/mac/scoped_cftyperef.h"

@class ManualTextFramer;

namespace core_text_util {

// Uses CTFrameSetter to create a CTFrameRef corresponding to |string| drawn
// within the path described by |bounds|.
base::ScopedCFTypeRef<CTFrameRef> CreateTextFrameForStringInBounds(
    NSAttributedString* string,
    CGRect bounds);

// The CTFrames returned by CTFameSetter sometimes erroneously frame the same
// portion of the original string twice. In addition, these CTFrames may have
// innacurate ranges for RTL languages. This function returns true if the
// string range for |text_frame| returned by CTFrameGetVisibleStringRange() is
// properly framed with no repeats and if the string range length of
// |text_frame| matches the string range length of the frame generated by
// |manual_framer|, which more accurately handles creating string ranges for RTL
// strings. |string| is the attributed string from which |text_frame| and
// |manual_framer| were created.
bool IsTextFrameValid(CTFrameRef text_frame,
                      ManualTextFramer* manual_framer,
                      NSAttributedString* string);

// Returns the width of the typographic bounds for |line|, excluding trailing
// whitespace.
CGFloat GetTrimmedLineWidth(CTLineRef line);

// Returns the width of the typographic bounds for the glyphs in |range| for
// |run|.
CGFloat GetRunWidthWithRange(CTRunRef run, CFRange range);

// Returns the width of the typographic bounds for the glyph in |run| at
// |glyph_idx|.
CGFloat GetGlyphWidth(CTRunRef run, CFIndex glyph_idx);

// Returns the index of the first glyph in the leading direction in |range| for
// |run| that corresponds with a character that is a member of |set|.  |string|
// is the string from which |run| was created, and is used to map from glyphs to
// characters.  Returns kCFNotFound if no glyphs are found.
CFIndex GetGlyphIdxForCharInSet(CTRunRef run,
                                CFRange range,
                                NSAttributedString* string,
                                NSCharacterSet* set);

// Returns the index of the character corresponding with the glyph at
// |glyph_idx| in |run|.
NSUInteger GetStringIdxForGlyphIdx(CTRunRef run, CFIndex glyph_idx);

// Returns the string range associated with |run|.
NSRange GetStringRangeForRun(CTRunRef run);

// Enumerates each attributes dictionary for |range| in |string| and executes
// |block| on the dictionary.
typedef void (^AttributesBlock)(NSDictionary* attributes);
void EnumerateAttributesInString(NSAttributedString* string,
                                 NSRange range,
                                 AttributesBlock block);

// Returns the line height for |range| in |string|.
CGFloat GetLineHeight(NSAttributedString* string, NSRange range);

// Returns the line spacing for |range| in |string|.
CGFloat GetLineSpacing(NSAttributedString* string, NSRange range);

// Returns the effective text alignment for |string|.
NSTextAlignment GetEffectiveTextAlignment(NSAttributedString* string);

// Returns the effective writing direction for |string|.
NSWritingDirection GetEffectiveWritingDirection(NSAttributedString* string);

}  // namespace core_text_util

#endif  // IOS_CHROME_BROWSER_UI_UTIL_CORE_TEXT_UTIL_H_
